package com.hashwolf.newpipe.settings;

import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.preference.PreferenceManager;

import com.hashwolf.newpipe.App;
import com.hashwolf.newpipe.R;

import static com.hashwolf.newpipe.MainActivity.DEBUG;

/**
 * 这个类处理preference不同版本间的数据迁移和从preference同步设置数据
 */
public class SettingMigrations {
    private static final String TAG = SettingMigrations.class.toString();

    private static SharedPreferences sp;

    /**
     * preference的版本号
     */
    private static final int VERSION = 6;

    abstract static class Migration {
        public final int oldVersion;
        public final int newVersion;

        protected Migration(final int oldVersion, final int newVersion) {
            this.oldVersion = oldVersion;
            this.newVersion = newVersion;
        }

        /**
         * 老版本大于等于当前版本则需要迁移
         *
         * @param currentVersion 当前配置的版本号
         * @return 是否应该迁移
         */
        private boolean shouldMigrate(final int currentVersion) {
            return oldVersion >= currentVersion;
        }

        protected abstract void migrate(@NonNull Context contex);
    }

    private static final Migration MIGRATION_5_6 = new Migration(5, 6) {
        @Override
        protected void migrate(@NonNull final Context context) {
            final boolean loadImages = sp.getBoolean("download_thumbnail_key", true);
            sp.edit().putString(context.getString(R.string.image_quality_key), context.getString(loadImages ? R.string.image_quality_medium : R.string.image_quality_none)).apply();
        }
    };

    private static final Migration[] SETTING_MIGRATIONS = {
            MIGRATION_5_6
    };

    public static void runMigrationsIfNeeded(@NonNull final Context context) {
        // 开始数据迁移并检查版本
        sp = PreferenceManager.getDefaultSharedPreferences(context);
        final String lastPreVersionKey = context.getString(R.string.last_used_preferences_version);
        final int lastPrefVersion = sp.getInt(lastPreVersionKey, 0);

        // 无需迁移开始同步设置
        if (App.getApp().isFirstRun()) {
            sp.edit().putInt(lastPreVersionKey, VERSION).apply();
            return;
        } else if (lastPrefVersion == VERSION) {
            return;
        }

        // 运行迁移
        int currentVersion = lastPrefVersion;
        for (final Migration currentMigration : SETTING_MIGRATIONS) {
            try {
                if (currentMigration.shouldMigrate(currentVersion)) {
                    if (DEBUG) {
                        Log.d(TAG, "Migrating preferences from version "
                                + currentVersion + " to " + currentMigration.newVersion);
                    }
                    currentMigration.migrate(context);
                    currentVersion = currentMigration.newVersion;
                }
            } catch (final Exception e) {
                sp.edit().putInt(lastPreVersionKey, currentVersion).apply();
                // TODO: 开启错误activiy
                return;
            }
        }
        // 保存当前preference的版本
        sp.edit().putInt(lastPreVersionKey, currentVersion).apply();
    }
}
